//
//  AppDelegate.m
//  HanlinWeather
//
//  Created by 任翰林 on 17/1/10.
//  Copyright © 2017年 任翰林. All rights reserved.
//

#import "AppDelegate.h"
#import "HLTabBarViewController.h"
#import <RESideMenu/RESideMenu.h>
#import "HLHomeViewController.h"
#import "HLNewsViewController.h"
#import "HLBaseNavigationController.h"
#import "HLTest3DtouchVCone.h"
#import "HLTest3DtouchVCtwo.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <SMS_SDK/SMSSDK.h>
@interface AppDelegate ()<UIApplicationDelegate>
@property(nonatomic,strong)HLTabBarViewController * tabBarVC;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [AMapServices sharedServices].apiKey = @"1b94333771289ef508c2d3e509fc6743";
    
    self.window = [[UIWindow alloc]initWithFrame:KScreenBounds];
    self.window.rootViewController = [[HLTabBarViewController alloc]init];
    self.tabBarVC = (HLTabBarViewController *)self.window.rootViewController;
    [self.window makeKeyAndVisible];
    
    
    
//    DEMONavigationController *navigationController = [[DEMONavigationController alloc] initWithRootViewController:[[DEMOHomeViewController alloc] init]];
//    DEMOLeftMenuViewController *leftMenuViewController = [[DEMOLeftMenuViewController alloc] init];
//    DEMORightMenuViewController *rightMenuViewController = [[DEMORightMenuViewController alloc] init];
//    
//    // Create side menu controller
//    //
//    RESideMenu *sideMenuViewController = [[RESideMenu alloc] initWithContentViewController:navigationController
//                                                                    leftMenuViewController:leftMenuViewController
//                                                                   rightMenuViewController:rightMenuViewController];
//    sideMenuViewController.backgroundImage = [UIImage imageNamed:@"Stars"];
//    
//    // Make it a root controller
//    //
//    self.window.rootViewController = sideMenuViewController;
    

    [self Test3DTouch:application];
    
    
    [SMSSDK registerApp:@"1ce71de3c3cfd"
             withSecret:@"e1e68864403bf1228e26136749756185"];
    
    
    return YES;
}




- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
    [self saveContext];
}


#pragma mark - 测试3D Touch
//首先在AppDelegate的didFinishLaunchingWithOptions里面初始化,注意这里是在didFinishLaunchingWithOptions里面调用的这个方法。

- (void)Test3DTouch:(UIApplication*)applecation
{

//    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//    
//    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[[UIViewController alloc]init]];
//    self.window.rootViewController = nav;
//    
//    [self.window makeKeyAndVisible];

    [self setup3DTouch:applecation];
}



- (void)setup3DTouch:(UIApplication *)application
{
    /**
     type 该item 唯一标识符
     localizedTitle ：标题
     localizedSubtitle：副标题
     icon：icon图标 可以使用系统类型 也可以使用自定义的图片
     userInfo：用户信息字典 自定义参数，完成具体功能需求
     */
    //    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"标签.png"];
    UIApplicationShortcutIcon *cameraIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeHome];
    UIApplicationShortcutItem *cameraItem = [[UIApplicationShortcutItem alloc] initWithType:@"ONE" localizedTitle:@"进入首页" localizedSubtitle:@"" icon:cameraIcon userInfo:nil];
    
    UIApplicationShortcutIcon *shareIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePlay];
    UIApplicationShortcutItem *shareItem = [[UIApplicationShortcutItem alloc] initWithType:@"TWO" localizedTitle:@"进入第二页" localizedSubtitle:@"" icon:shareIcon userInfo:nil];
    /** 将items 添加到app图标 */
    application.shortcutItems = @[cameraItem,shareItem];
}
//最后在代理方法里面实现具体需求代码
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    
//    HLBaseNavigationController *nav = (HLBaseNavigationController *)self.window.rootViewController;
    
    HLBaseNavigationController *nav1 = self.tabBarVC.childViewControllers[0];
    HLBaseNavigationController *nav2 = self.tabBarVC.childViewControllers[1];
//    [self.tabBarVC addChildViewController:nav];
    if([shortcutItem.type isEqualToString:@"ONE"]){
        HLTest3DtouchVCone *vc = [[HLTest3DtouchVCone alloc] init];
        vc.title = @"进入首页";
        vc.view.backgroundColor = [UIColor redColor];
        [nav1 pushViewController:vc animated:YES];
    }else if ([shortcutItem.type isEqualToString:@"TWO"]){
        HLTest3DtouchVCtwo *vc = [[HLTest3DtouchVCtwo alloc] init];
        vc.title = @"进入第二页";
        vc.view.backgroundColor = [UIColor greenColor];
        [nav2 pushViewController:vc animated:YES];
        self.tabBarVC.selectedIndex = 1;
    }
}

#pragma mark - Core Data stack

@synthesize persistentContainer = _persistentContainer;

- (NSPersistentContainer *)persistentContainer {
    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
    @synchronized (self) {
        if (_persistentContainer == nil) {
            _persistentContainer = [[NSPersistentContainer alloc] initWithName:@"HanlinWeather"];
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
                if (error != nil) {
                    // Replace this implementation with code to handle the error appropriately.
                    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                    
                    /*
                     Typical reasons for an error here include:
                     * The parent directory does not exist, cannot be created, or disallows writing.
                     * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                     * The device is out of space.
                     * The store could not be migrated to the current model version.
                     Check the error message to determine what the actual problem was.
                    */
                    NSLog(@"Unresolved error %@, %@", error, error.userInfo);
                    abort();
                }
            }];
        }
    }
    
    return _persistentContainer;
}

#pragma mark - Core Data Saving support

- (void)saveContext {
    NSManagedObjectContext *context = self.persistentContainer.viewContext;
    NSError *error = nil;
    if ([context hasChanges] && ![context save:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, error.userInfo);
        abort();
    }
}






@end
